#region License, Terms and Conditions
//
// Prawks - Copyright (c) 2007 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

namespace Prawks.CodeProvisioning
{
    using System;

    using Debug = System.Diagnostics.Debug;
    using ILGenerator = System.Reflection.Emit.ILGenerator;
    using OpCodes = System.Reflection.Emit.OpCodes;
    
    internal sealed class ILUtility
    {
        private ILUtility()
        {
            throw new NotSupportedException();
        }

        internal static bool TryEmitLdind(ILGenerator il, Type type)
        {
            Debug.Assert(il != null);
            Debug.Assert(type != null);

            try
            {
                EmitLdind(il, type);
                return true;
            }
            catch (NotSupportedException)
            {
                return false;
            }
        }

        internal static void EmitLdind(ILGenerator il, Type type)
        {
            Debug.Assert(il != null);
            Debug.Assert(type != null);

            if (type.IsValueType)
            {
                switch (Type.GetTypeCode(type))
                {
                    case TypeCode.Boolean   : il.Emit(OpCodes.Ldind_I1); break;
                    case TypeCode.Byte      : il.Emit(OpCodes.Ldind_U1); break;
                    case TypeCode.Int16     : il.Emit(OpCodes.Ldind_I2); break;
                    case TypeCode.Int32     : il.Emit(OpCodes.Ldind_I4); break;
                    case TypeCode.Int64     : il.Emit(OpCodes.Ldind_I8); break;
                    case TypeCode.Single    : il.Emit(OpCodes.Ldind_R4); break;
                    case TypeCode.Double    : il.Emit(OpCodes.Ldind_R8); break;
                    case TypeCode.Decimal   :
                    case TypeCode.DateTime  :
                    case TypeCode.Object    : il.Emit(OpCodes.Ldobj, type); break;
            
                    default : throw new NotSupportedException();
                }
            }
            else
            {
                il.Emit(OpCodes.Ldind_Ref);
            }
        }

        internal static bool TryEmitStind(ILGenerator il, Type type)
        {
            Debug.Assert(il != null);
            Debug.Assert(type != null);

            try
            {
                EmitStind(il, type);
                return true;
            }
            catch (NotSupportedException)
            {
                return false;
            }
        }

        internal static void EmitStind(ILGenerator il, Type type)
        {
            Debug.Assert(il != null);
            Debug.Assert(type != null);

            if (type.IsValueType)
            {
                switch (Type.GetTypeCode(type))
                {
                    case TypeCode.Boolean   : il.Emit(OpCodes.Stind_I1); break;
                    case TypeCode.Byte      : il.Emit(OpCodes.Stind_I1); break;
                    case TypeCode.Int16     : il.Emit(OpCodes.Stind_I2); break;
                    case TypeCode.Int32     : il.Emit(OpCodes.Stind_I4); break;
                    case TypeCode.Int64     : il.Emit(OpCodes.Stind_I8); break;
                    case TypeCode.Single    : il.Emit(OpCodes.Stind_R4); break;
                    case TypeCode.Double    : il.Emit(OpCodes.Stind_R8); break;
                    case TypeCode.Decimal   :
                    case TypeCode.DateTime  :
                    case TypeCode.Object    : il.Emit(OpCodes.Stobj, type); break;
                
                    default : throw new NotSupportedException();
                }
            }
            else
            {
                il.Emit(OpCodes.Stind_Ref);
            }
        }
    }
}
